var util = require('/util.js')

var baseWoyesurl = 'http://testwo.woyes.com';
var baseFqurl = 'http://fq.woyes.com';
var encryption = require('/encryption.js');

var securityKey = "5F2030CB850D95E5EE4193E56F3FBAA5";//掌沃生活 app 验签key

// var commonParameters = {//baseWoyesurl
//     platformId: "0757000002",
//     channelId: "0757000002_100062",
//     terminalId: "zhangwo",
//     signMethod: "MD5",
//     dataType: "json",
//     version: "1.0.0"
// }

// var commonParametersByFqUrl = {//baseFqurl
//     platformId: "1000000023",
//     channelId: "00",
//     terminalId: "fenqigou",
//     signMethod: "MD5",
//     dataType: "json",
//     version: "1.0.0"
// }

function startWith(str, prefix) {
    if (str == null) {
        return false;
    }
    return str.indexOf(prefix) == 0;
}

function getCommonParameter(url) {
    if (startWith(url, baseWoyesurl)) {
        var _commonParameters = {//baseWoyesurl
            platformId: "0757000002",
            channelId: "0757000002_100062",
            terminalId: "zhangwo",
            signMethod: "MD5",
            dataType: "json",
            version: "1.0.0"
        }
        _commonParameters.reqtime = util.formatDate(new Date(), 'yyyy-MM-dd/HH:mm:ss');
        return _commonParameters;
    } else if (startWith(url, baseFqurl)) {
        var _commonParametersByFqUrl = {//baseFqurl
            platformId: "1000000023",
            channelId: "00",
            terminalId: "fenqigou",
            signMethod: "MD5",
            dataType: "json",
            version: "1.0.0"
        }
        _commonParametersByFqUrl.reqtime = util.formatDate(new Date(), 'yyyy-MM-dd/HH:mm:ss');
        return _commonParametersByFqUrl;
    } else {
        return null;
    }
}

function getSign(obj) {//验签方法
    var tmp = '';
    var tmpJson = {};
    //排序
    Object.keys(obj).sort().forEach(function (k) { tmpJson[k] = obj[k] });
    for (var item in tmpJson) {
        var jValue = tmpJson[item];//key所对应的value  
        //console.log(item+":"+jValue);
        tmp = tmp + item + "=" + jValue + "&";
    }
    tmp = tmp + encryption.MD5(securityKey);
    return encryption.MD5(tmp);
}

module.exports = {
    baseWoyesurl: baseWoyesurl,
    baseFqurl: baseFqurl,
    getCommonParameter: getCommonParameter,
    getSign: getSign
}